package com.att.action;

import com.adkfp.utils.LayuiUtil;
import com.att.bean.SchClassBean;
import com.att.service.SchClassService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("schClass")
public class SchClassAction {

    @Resource(name = "schClassService")
    private SchClassService schClassService;

    /**
     * Description: 带条件查询时间段维护记录
     *
     * @param bean
     */
    @RequestMapping("selectAll")
    @ResponseBody
    public Object selectAll(SchClassBean bean) {
        SimpleDateFormat format2 = new SimpleDateFormat("HH:mm:ss");
        List<SchClassBean> list = schClassService.findByCondition(bean);
        for (int i = 0; i < list.size(); i++) {

//            String startTime = CalendarUtil.time(list.get(i).getStartTime());
//            String endTime = CalendarUtil.time(list.get(i).getEndTime());
//            String checkInTime1 = CalendarUtil.time(list.get(i).getCheckInTime1());
//            String checkInTime2 = CalendarUtil.time(list.get(i).getCheckInTime2());
//            String checkOutTime1 = CalendarUtil.time(list.get(i).getCheckOutTime1());
//            String checkOutTime2 = CalendarUtil.time(list.get(i).getCheckOutTime2());

            String startTime = format2.format(list.get(i).getStartTime());
            String endTime = format2.format(list.get(i).getEndTime());
            String checkInTime1 = format2.format(list.get(i).getCheckInTime1());
            String checkInTime2 = format2.format(list.get(i).getCheckInTime2());
            String checkOutTime1 = format2.format(list.get(i).getCheckOutTime1());
            String checkOutTime2 = format2.format(list.get(i).getCheckOutTime2());
            list.get(i).setStartTimeStr(startTime.substring(0,startTime.lastIndexOf(":")));
            list.get(i).setEndTimeStr(endTime.substring(0,endTime.lastIndexOf(":")));
            list.get(i).setCheckInTime1Str(checkInTime1.substring(0,checkInTime1.lastIndexOf(":")));
            list.get(i).setCheckInTime2Str(checkInTime2.substring(0,checkInTime2.lastIndexOf(":")));
            list.get(i).setCheckOutTime1Str(checkOutTime1.substring(0,checkOutTime1.lastIndexOf(":")));
            list.get(i).setCheckOutTime2Str(checkOutTime2.substring(0,checkOutTime2.lastIndexOf(":")));
        }
        return LayuiUtil.data(Long.valueOf(list.size()), list);
    }

    /**
     * Description: 转发到页面
     */
    @RequestMapping("schClassIndex")
    public String schClassIndex() {
        return "settings/schClass";
    }

    //新增
    @RequestMapping("insert")
    @ResponseBody
    public String insert(SchClassBean bean){
        boolean flag = schClassService.insert(bean);
        if (flag) {
            return "SUCCESS";
        }
        return "ERROR";
    }

    //修改
    @RequestMapping("update")
    @ResponseBody
    public String update(SchClassBean bean) {
        boolean flag = schClassService.update(bean);
        if (flag) {
            return "SUCCESS";
        }
        return "ERROR";
    }

    //删除
    @RequestMapping("delete")
    @ResponseBody
    public String delete(Integer id) {
        boolean flag = schClassService.delete(id);
        if (flag) {
            return "SUCCESS";
        }
        return "ERROR";
    }

    /**
     * Description: 查询时段名称
     */
    @RequestMapping("findSchClassName")
    @ResponseBody
    public Object findSchClassName(SchClassBean bean) {
        List<SchClassBean> list = schClassService.findSchClassName(bean);
        return list;
    }
}
